package test;

import java.awt.Dimension;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.NumberTickUnit;
import org.jfree.chart.axis.TickUnit;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;

import core.Statistics;

public class LineChartDemo2 extends ApplicationFrame {
	public LineChartDemo2(List<Statistics> list, String paramString) {
		super(paramString);
		JPanel localJPanel = createDemoPanel(list);
		//JPanel localJPanel2 = createDemoPanel2(list);
		JPanel localJPanel3 = createDemoPanel3(list);

		//localJPanel.setPreferredSize(new Dimension(500, 270));
		//localJPanel2.setPreferredSize(new Dimension(500, 270));
		JPanel f = new JPanel();
	//	f.add(localJPanel2);
		f.add(localJPanel);
		f.add(localJPanel3);
		add(f);

	}

	private static XYDataset createDatasetFromStatistics(List<Statistics> list) {
		XYSeries localXYSeries1 = new XYSeries("Mejor Error del individuo de la Poblacion");
		XYSeries localXYSeries2 = new XYSeries("Promedio de los Errores de la Poblacion");
		for (int i = 0; i < list.size(); i++){
			localXYSeries1.add(i, list.get(i).findBest());
			localXYSeries2.add(i, list.get(i).getAvg());
		}
		
		XYSeriesCollection localXYSeriesCollection = new XYSeriesCollection();
		localXYSeriesCollection.addSeries(localXYSeries1);
		localXYSeriesCollection.addSeries(localXYSeries2);

		return localXYSeriesCollection;

	}
	
	private static XYDataset createDatasetMinFromStatistics(List<Statistics> list) {
		XYSeries localXYSeries1 = new XYSeries("Mejor Error del individuo de la Poblacion");
		for (int i = 0; i < list.size(); i++){
			localXYSeries1.add(i, list.get(i).findBest());
		}
		
		XYSeriesCollection localXYSeriesCollection = new XYSeriesCollection();
		localXYSeriesCollection.addSeries(localXYSeries1);

		return localXYSeriesCollection;

	}
	
	private static XYDataset createDatasetVariationFromStatistics(List<Statistics> list) {
		XYSeries localXYSeries1 = new XYSeries("Variacion");
		for (int i = 0; i < list.size(); i++){
			localXYSeries1.add(i, list.get(i).getVariation());
		}
		XYSeriesCollection localXYSeriesCollection = new XYSeriesCollection();
		localXYSeriesCollection.addSeries(localXYSeries1);
		return localXYSeriesCollection;

	}

	private static XYDataset createDataset() {
		XYSeries localXYSeries1 = new XYSeries("First");
		localXYSeries1.add(1.0D, 1.0D);
		localXYSeries1.add(2.0D, 4.0D);
		localXYSeries1.add(3.0D, 3.0D);
		localXYSeries1.add(4.0D, 5.0D);
		localXYSeries1.add(5.0D, 5.0D);
		localXYSeries1.add(6.0D, 7.0D);
		localXYSeries1.add(7.0D, 7.0D);
		localXYSeries1.add(8.0D, 8.0D);
		XYSeries localXYSeries2 = new XYSeries("Second");
		localXYSeries2.add(1.0D, 5.0D);
		localXYSeries2.add(2.0D, 7.0D);
		localXYSeries2.add(3.0D, 6.0D);
		localXYSeries2.add(4.0D, 8.0D);
		localXYSeries2.add(5.0D, 4.0D);
		localXYSeries2.add(6.0D, 4.0D);
		localXYSeries2.add(7.0D, 2.0D);
		localXYSeries2.add(8.0D, 1.0D);
		XYSeries localXYSeries3 = new XYSeries("Third");
		localXYSeries3.add(3.0D, 4.0D);
		localXYSeries3.add(4.0D, 3.0D);
		localXYSeries3.add(5.0D, 2.0D);
		localXYSeries3.add(6.0D, 3.0D);
		localXYSeries3.add(7.0D, 6.0D);
		localXYSeries3.add(8.0D, 3.0D);
		localXYSeries3.add(9.0D, 4.0D);
		localXYSeries3.add(10.0D, 3.0D);
		XYSeriesCollection localXYSeriesCollection = new XYSeriesCollection();
		localXYSeriesCollection.addSeries(localXYSeries1);
		localXYSeriesCollection.addSeries(localXYSeries2);
		localXYSeriesCollection.addSeries(localXYSeries3);
		return localXYSeriesCollection;
	}

	private static XYDataset createNDataset() {
		XYSeries localXYSeries1 = new XYSeries("First");
		localXYSeries1.add(10.0D, 1.0D);
		localXYSeries1.add(21.0D, 4.0D);
		localXYSeries1.add(31.0D, 3.0D);
		localXYSeries1.add(41.0D, 5.0D);
		localXYSeries1.add(51.0D, 5.0D);
		localXYSeries1.add(61.0D, 7.0D);
		localXYSeries1.add(71.0D, 7.0D);
		localXYSeries1.add(81.0D, 8.0D);
		XYSeries localXYSeries2 = new XYSeries("Second");
		localXYSeries2.add(11.0D, 5.0D);
		localXYSeries2.add(21.0D, 7.0D);
		localXYSeries2.add(31.0D, 6.0D);
		localXYSeries2.add(41.0D, 8.0D);
		localXYSeries2.add(51.0D, 4.0D);
		localXYSeries2.add(61.0D, 4.0D);
		localXYSeries2.add(71.0D, 2.0D);
		localXYSeries2.add(81.0D, 1.0D);
		XYSeries localXYSeries3 = new XYSeries("Third");
		localXYSeries3.add(31.0D, 4.0D);
		localXYSeries3.add(41.0D, 3.0D);
		localXYSeries3.add(51.0D, 2.0D);
		localXYSeries3.add(61.0D, 3.0D);
		localXYSeries3.add(71.0D, 6.0D);
		localXYSeries3.add(81.0D, 3.0D);
		localXYSeries3.add(91.0D, 4.0D);
		localXYSeries3.add(10.0D, 3.0D);
		XYSeriesCollection localXYSeriesCollection = new XYSeriesCollection();
		localXYSeriesCollection.addSeries(localXYSeries1);
		localXYSeriesCollection.addSeries(localXYSeries2);
		localXYSeriesCollection.addSeries(localXYSeries3);
		return localXYSeriesCollection;
	}

	private static JFreeChart createChart(XYDataset paramXYDataset) {
		JFreeChart localJFreeChart = ChartFactory.createXYLineChart(
				"Errores", "Generaciones", "Valor error", paramXYDataset,
				PlotOrientation.VERTICAL, true, true, false);
		XYPlot localXYPlot = (XYPlot) localJFreeChart.getPlot();
		localXYPlot.setDomainPannable(true);
		localXYPlot.setRangePannable(true);
		XYLineAndShapeRenderer localXYLineAndShapeRenderer = (XYLineAndShapeRenderer) localXYPlot
				.getRenderer();
		//localXYLineAndShapeRenderer.setBaseShapesVisible(true);
		//localXYLineAndShapeRenderer.setBaseShapesFilled(true);
		NumberAxis localNumberAxis = (NumberAxis) localXYPlot.getRangeAxis();
		//localNumberAxis.setStandardTickUnits(NumberAxis
		//		.createIntegerTickUnits());
		localNumberAxis.setAutoTickUnitSelection(true);
		//localNumberAxis.setTickUnit(new NumberTickUnit(0.1D));
		//localNumberAxis.setAutoRangeIncludesZero(true);
		return localJFreeChart;
	}

	private static JFreeChart createChart2(XYDataset paramXYDataset) {
		JFreeChart localJFreeChart = ChartFactory.createXYLineChart(
				"Variacion de generacion entre generacion", "Generaciones", "Cantidad de individuos cambiados", paramXYDataset,
				PlotOrientation.VERTICAL, true, true, false);
		XYPlot localXYPlot = (XYPlot) localJFreeChart.getPlot();
		localXYPlot.setDomainPannable(true);
		localXYPlot.setRangePannable(true);
		XYLineAndShapeRenderer localXYLineAndShapeRenderer = (XYLineAndShapeRenderer) localXYPlot
				.getRenderer();
		localXYLineAndShapeRenderer.setBaseShapesVisible(true);
		//localXYLineAndShapeRenderer.setBaseShapesFilled(true);
		NumberAxis localNumberAxis = (NumberAxis) localXYPlot.getRangeAxis();
		localNumberAxis.setStandardTickUnits(NumberAxis
				.createIntegerTickUnits());
		localNumberAxis.setTickUnit(new NumberTickUnit(0.3D));
		localNumberAxis.setAutoRangeIncludesZero(true);
		return localJFreeChart;
	}

	private static JFreeChart createChart3(XYDataset paramXYDataset) {
		JFreeChart localJFreeChart = ChartFactory.createXYLineChart(
				"Error minimo en generaciones","Generaciones","valor error", paramXYDataset,
				PlotOrientation.VERTICAL, true, true, false);
		XYPlot localXYPlot = (XYPlot) localJFreeChart.getPlot();
		localXYPlot.setDomainPannable(true);
		localXYPlot.setRangePannable(true);
		XYLineAndShapeRenderer localXYLineAndShapeRenderer = (XYLineAndShapeRenderer) localXYPlot
				.getRenderer();
		localXYLineAndShapeRenderer.setBaseShapesVisible(true);
		//localXYLineAndShapeRenderer.setBaseShapesFilled(true);
		NumberAxis localNumberAxis = (NumberAxis) localXYPlot.getRangeAxis();
		//localNumberAxis.setStandardTickUnits(NumberAxis
		 	//	.createIntegerTickUnits());
		localNumberAxis.setAutoTickUnitSelection(true);

		return localJFreeChart;
	}
	public static JPanel createDemoPanel(List<Statistics> list) {
		JFreeChart localJFreeChart = createChart(createDatasetFromStatistics(list));
		ChartPanel localChartPanel = new ChartPanel(localJFreeChart);
		localChartPanel.setMouseZoomable(true, false);
		localChartPanel.setMouseWheelEnabled(true);
		return localChartPanel;
	}

	public static JPanel createDemoPanel2(List<Statistics> list) {
		JFreeChart localJFreeChart = createChart2(createDatasetVariationFromStatistics(list));
		ChartPanel localChartPanel = new ChartPanel(localJFreeChart);
		localChartPanel.setMouseZoomable(true, false);
		localChartPanel.setMouseWheelEnabled(true);
		return localChartPanel;
	}

	public static JPanel createDemoPanel3(List<Statistics> list) {
		JFreeChart localJFreeChart = createChart3(createDatasetMinFromStatistics(list));
		ChartPanel localChartPanel = new ChartPanel(localJFreeChart);
		localChartPanel.setMouseZoomable(true, false);
		localChartPanel.setMouseWheelEnabled(true);
		return localChartPanel;
	}
	public static void main(String[] paramArrayOfString) {
		LineChartDemo2 localLineChartDemo2 = new LineChartDemo2(null,
				"JFreeChart: LineChartDemo2.java");
		localLineChartDemo2.pack();
		RefineryUtilities.centerFrameOnScreen(localLineChartDemo2);
		localLineChartDemo2.setVisible(true);
	}
}